package com.sdy.healthcode.h5.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.MapUtil;
import com.sdy.healthcode.biz.dto.cockpit.CockpitAppointmentStatistic;
import com.sdy.healthcode.biz.dto.cockpit.CockpitClockInStatistic;
import com.sdy.healthcode.biz.dto.cockpit.CockpitExpertsAppointment;
import com.sdy.healthcode.biz.model.ReportPeopleClock;
import com.sdy.healthcode.biz.service.ExpertsAppointmentService;
import com.sdy.healthcode.biz.service.HealthCodeService;
import com.sdy.healthcode.biz.service.ReportPeopleClockService;
import com.sdy.healthcode.biz.service.UserActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/cockpit")
@Api(tags = "驾驶舱")
public class CockpitController {
    @Autowired
    private ExpertsAppointmentService expertsAppointmentService;
    @Autowired
    private HealthCodeService healthCodeService;
    @Autowired
    private ReportPeopleClockService reportPeopleClockService;
    @Autowired
    private UserActionService userActionService;
    
    @GetMapping("/data")
    @ApiOperation(value = "驾驶舱数据")
    public Response data() {
        Map<String, Object> map = new HashMap<>();
        Date now = new Date();
        map.put("appointmentProfile", appointmentProfile(now));
        map.put("deptAppointment", appointmentDataMap());
        map.put("healthCode", healthCodeMap());
        map.put("clockIn", clockInMap(now));
        map.put("updateTime", DateUtil.getDate(now, "HH:mm:ss"));
        return Response.success(map);
    }

    /**
     * 健康码分布
     */
    private Map<String, Object> appointmentProfile(Date now) {
        Map<String, Object> map = new HashMap<>();
        Date yesterday = DateUtil.addDate(now, -1);
        // 预约就诊人数
        int todayAppointment = userActionService.countHospitalIn(now);
        int yesterdayAppointment = userActionService.countHospitalIn(yesterday);
        // 到院人数
        int todayArrived = userActionService.countHospitalOut(now);
        int yesterdayArrived = userActionService.countHospitalOut(yesterday);
        // 取消就诊
        int todayAppointmentCanceled = userActionService.countAppointmentCanceled(now);
        int yesterdayAppointmentCanceled = userActionService.countAppointmentCanceled(yesterday);
        map.put("todayAppointment", todayAppointment);
        map.put("todayAppointmentAdd", todayAppointment - yesterdayAppointment);
        map.put("todayArrived", todayArrived);
        map.put("todayArrivedAdd", todayArrived - yesterdayArrived);
        map.put("todayAppointmentCanceled", todayAppointmentCanceled);
        map.put("todayAppointmentCanceledAdd", todayAppointmentCanceled - yesterdayAppointmentCanceled);
        return map;
    }

    /**
     * 科室挂号情况
     */
    private Map<String, Object> appointmentDataMap() {
        Map<String, Object> map = new HashMap<>();
        List<CockpitAppointmentStatistic> cockpitAppointmentStatistics = expertsAppointmentService.deptAppointmentMap();
        map.put("sum", cockpitAppointmentStatistics.stream().mapToInt(CockpitAppointmentStatistic::getCnt).sum());
        map.put("data", cockpitAppointmentStatistics);
        return map;
    }

    /**
     * 健康码分布
     */
    private Map<String, Object> healthCodeMap() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> hospitalNum = healthCodeService.getHospitalNum();
        map.put("data", hospitalNum);
        return map;
    }

    /**
     * 健康码分布
     */
    private Map<String, Object> clockInMap(Date now) {
        Map<String, Object> map = new HashMap<>();
        Date today = DateUtil.getDateBegin(now);
        Date startDate = DateUtil.addDate(today, -13);
        List<ReportPeopleClock> clockInList = reportPeopleClockService.lambdaQuery()
                .between(ReportPeopleClock::getRecordDate,
                        DateUtil.getDate(startDate, "yyyyMMdd"),
                        DateUtil.getDate(today, "yyyyMMdd"))
                .list();
        Map<String, ReportPeopleClock> clockMap = MapUtil.collectionToMap(clockInList, ReportPeopleClock::getRecordDate);
        List<CockpitClockInStatistic> r = new ArrayList<>();
        for (int i = 0; i < 14; i++) {
            String dateStr = DateUtil.getDate(DateUtil.addDate(startDate, i), "yyyyMMdd");
            ReportPeopleClock c = clockMap.getOrDefault(dateStr,
                    new ReportPeopleClock().setAlreadyClockNum(0));
            r.add(new CockpitClockInStatistic().setDate(dateStr).setCnt(c.getAlreadyClockNum()));
        }
        map.put("data", r);
        return map;
    }

    @GetMapping("/pageApplyCancelInfo")
    @ApiOperation("科室取消预约分页信息")
    public Response<IPage<CockpitExpertsAppointment>> pageApplyCancelInfo(Page<CockpitExpertsAppointment> page) {
        return Response.success(expertsAppointmentService.pageExpertsAppointmentInfo(page, 2, new Date()));
    }
}
